package org.itboys.mobile.service.mongo.mobile;

import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayAddressDto;
import org.itboys.mobile.entity.mongo.SundayAddress;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_收获地址_service_移动端
 */
@Service
public class SundayMobileAddressService extends BaseMongoService<SundayAddress> {



    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayAddress> getEntityClass() {
        return SundayAddress.class;
    }

    /**
     * 获取用户所有收货地址
     * @param memberId
     * @return
     */
    public List<SundayAddressDto> getAddress(Long memberId){
        Map<String,Object> param =new HashMap<String, Object>();
        param.put("memberId", memberId);
        param.put("isDeleted", SundayCommonConstants.NO);
        //优先排序默认地址
        param.put("sort","isDefault");
        param.put("order","DESC");

        return packageDtoBatch(super.list(param));
    }

    /**
     * 新增收货地址(用户版和管理版)。农厨版本收货地址：用户：传入小区ID；门店（大客户）：传入省市区
     * @param id
     * @param memberId
     * @param receiveName
     * @param receiveMobile
     * @param provinceId
     * @param provinceName
     * @param cityId
     * @param cityName
     * @param districtId
     * @param districtName
     * @param receiveAddress
     */
    public void saveAddress(Long id,Long memberId,String receiveName,String receiveMobile,
                            Long provinceId, String provinceName, Long cityId, String cityName, Long districtId, String districtName,
                            String receiveAddress){


        if(id==null||id==0l){
                SundayAddress address=new SundayAddress(
                        id,memberId, receiveName,receiveMobile,
                        provinceId, provinceName,cityId,cityName,districtId,districtName,
                        receiveAddress, SundayCommonConstants.NO);
                super.save(address);

        }else {
                super.updateExceptEmpty(id,new SundayAddress(id,memberId,receiveName,receiveMobile,provinceId, provinceName,cityId,cityName,districtId,districtName,receiveAddress, SundayCommonConstants.NO));
        }
    }

    /**
     * 设置默认地址
     * @param addressId
     * @param memberId
     */
    public void setDefaultAddress(Long addressId,Long memberId){
        //2017年6月20日，新增，验证签名
        Map<String, Object> param =new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId",memberId);
        List<SundayAddress> addresses = super.list(param);
        for(SundayAddress address:addresses){
            if(address.getId()==addressId){
                address.setIsDefault(SundayCommonConstants.YES);
                super.updateExceptEmpty(address.getId(),address);
            }else{
                address.setIsDefault(SundayCommonConstants.NO);
                super.updateExceptEmpty(address.getId(),address);
            }
        }
    }

    public SundayAddressDto packageDtoSingle(SundayAddress address){
        return new SundayAddressDto(address.getId(),address.getCreateTime(), address.getCreateDate(),
                address.getReceiveName(),address.getReceiveMobile(),
                address.getProvinceId(), address.getProvinceName(),address.getCityId(),address.getCityName(),address.getDistrictId(), address.getDistrictName(),
                address.getReceiveAddress(),address.getIsDefault());
    }
    public List<SundayAddressDto> packageDtoBatch(List<SundayAddress> addresses){
        List<SundayAddressDto> dtos = new ArrayList<>();
        for(SundayAddress address:addresses){
                dtos.add(packageDtoSingle(address));
        }
        return dtos;

    }
}
